package models;

import java.util.List;

import javax.persistence.Entity;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.ObjectNode;

import jiyici.Constant.ModelColumn;
import play.db.ebean.Model;

@Entity
public class RecordParam extends ServerBaseModel {

    private static final long serialVersionUID = 6570085354304912169L;

//    @ManyToOne
//    @JoinColumn(name = "record_id")
//    public Record record;
    
    public long recordId;
    
//    @ManyToOne
//    @JoinColumn(name = "item_param_id")
//    public ItemParam itemParam;
    public long itemParamId;

    public String value;
    
    public static Model.Finder<Long, RecordParam> find = new Model.Finder<Long, RecordParam>(Long.class, RecordParam.class);

    public static List<RecordParam> modifiedAfter(User user, long time) {
        return find.where().eq("modelId.userId", user.id).gt(ModelColumn.BASE_COLUMN_LASTMODIFIED, time)
                .findList();
    }
    
    @Override
    public ObjectNode toJson() {
        ObjectNode json = super.toJson();
        json.put(ModelColumn.RECORD_PARAM_RECORD_ID, recordId);
        json.put(ModelColumn.RECORD_PARAM_ITEM_PARAM_ID, itemParamId);
        json.put(ModelColumn.RECORD_PARAM_VALUE, value);
        return json;
    }
    
    @Override
    public void fromJson(JsonNode json) {
        super.fromJson(json);
        recordId = json.findPath(ModelColumn.RECORD_PARAM_RECORD_ID).getLongValue();
        itemParamId = json.findPath(ModelColumn.RECORD_PARAM_ITEM_PARAM_ID).getLongValue();
        value = json.findPath(ModelColumn.RECORD_PARAM_VALUE).getTextValue();
    }
}
